//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 8
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using ProxyHelpers.EWS;
using System.Net;

namespace CreateCalendarItem
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            ItemIdType newId = CreateCalendarItem(binding);
        }

        /// <summary>
        /// Create a calendar item (Listing 8-2)
        /// </summary>
        /// <param name="binding">Binding to use for web service call</param>
        /// <returns>Id of newly created calendar item</returns>
        /// 
        public static ItemIdType CreateCalendarItem(ExchangeServiceBinding binding)
        {
            CalendarItemType newCalendarItem = new CalendarItemType();
            newCalendarItem.Subject = "Write Chapter Explaining Calendar Items";
            CreateItemType createItemRequest = new CreateItemType();

            // When using the CreateItem web method to create a CalendarItem, we must
            // specify a value for the SendMeetingInviations attribute
            //
            createItemRequest.SendMeetingInvitations =
                    CalendarItemCreateOrDeleteOperationType.SendToNone;
            createItemRequest.SendMeetingInvitationsSpecified = true;

            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items = new CalendarItemType[1];
            createItemRequest.Items.Items[0] = newCalendarItem;
            // Make our request
            //
            CreateItemResponseType response = binding.CreateItem(createItemRequest);
            ItemInfoResponseMessageType responseMessage = response.ResponseMessages.Items[0] as
                  ItemInfoResponseMessageType;
            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception("CreateCalendarItem failed with response code " +
                         responseMessage.ResponseCode.ToString());
            }
            return responseMessage.Items.Items[0].ItemId;
        }

    }
}
